# -*- coding: utf8 -*-
# $Id :  $



# Copyright 2008-2010, Pascal Fautrero & JCharles
#
# This file is part of PyArchiver.
# 
# PyArchiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyArchiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyArchiver.  If not, see <http://www.gnu.org/licenses/>.



# Import packages

import wx                        # This module uses the new wx namespace
import sys
import os
import wx.lib.mixins.listctrl    as  listmix

# import images                  # For PyEmbeddedImage

_ = wx.GetTranslation            # Define _ or add _ to builtins in your app file

appDir = os.path.split(os.path.abspath(sys.argv[0]))[0]

#---------------------------------------------------------------------------

class My_ListCtrlFolders(wx.ListCtrl,
                         listmix.ListCtrlAutoWidthMixin):    
    def __init__(self, parent, frame):
        wx.ListCtrl.__init__(self, parent, -1,
                             style=wx.LC_REPORT | wx.LC_VIRTUAL |
                             wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
                             wx.LC_VRULES | wx.BORDER_SIMPLE)
        
        listmix.ListCtrlAutoWidthMixin.__init__(self)
        
        #-------------------------------------------------------------------

        fontSize = self.GetFont().GetPointSize()

        # wx.Font(pointSize, family, style, weight, underline, faceName)        
        if wx.Platform == "__WXMAC__":
            self.normalFont = wx.Font(fontSize, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
            self.SetFont(self.normalFont)

        elif wx.Platform == "__WXGTK__":
            self.normalFont = wx.Font(fontSize-1, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
            self.SetFont(self.normalFont)

        else:
            self.normalFont = wx.Font(fontSize-1, wx.DEFAULT, wx.NORMAL, wx.NORMAL)  # fontSize
            self.SetFont(self.normalFont)
            
        #-------------------------------------------------------------------
        
        self.il = wx.ImageList(16, 16)

        bitmapDir = os.path.join(appDir, "bitmaps")
        bmp = wx.Bitmap(os.path.join(bitmapDir, "listCtrl_Folder_Icon.png"))
        # or PyEmbeddedImage
        # bmp = images.ListCtrl_Folder_Icon.GetBitmap()
        self.idx1 = self.il.Add(bmp)
        
        self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
        
        #-------------------------------------------------------------------
        
        self.InsertColumn(0, "Dossiers", wx.LIST_FORMAT_LEFT, width=199)
        
        #-------------------------------------------------------------------
        
        self.SetItemCount(1000000)
        
        #-------------------------------------------------------------------
        
        self.SetForegroundColour("#6a6a66")
        
        self.attributeColor = wx.ListItemAttr()
        self.attributeColor.SetBackgroundColour(wx.Colour(224, 240, 240))
        
        #-------------------------------------------------------------------
        
        self.SetItemState(0, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
        
        #-------------------------------------------------------------------
        
        # Bind some events to an events handler
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
        self.NumberItems = 0
        self.parent = frame		
        
    #-----------------------------------------------------------------------

    def SpecialRefresh(self):
        self.Refresh()
        self.parent.text2.SetLabel(_("%d Folders. ") %self.NumberItems)		
		
	
    def OnItemSelected(self, event):
        self.currentItem = event.m_itemIndex
        self.parent.FilterSupports(self.GetItem(self.currentItem, 1).GetText())
        self.parent.SelectedSupport = ""
        self.parent.SelectedFolder = self.GetItem(self.currentItem, 1).GetText()


    def OnItemActivated(self, event):
        self.currentItem = event.m_itemIndex

        
    def getColumnText(self, index, col):
        item = self.GetItem(index, col)
        return item.GetText()
     
    #---------------------------------------------------
    # These methods are callbacks for implementing the
    # "virtualness" of the list...  Normally you would
    # determine the text, attributes and/or image based
    # on values from some external data source, but for
    # this demo we'll just calculate them
    def OnGetItemText(self, item, col):
        if hasattr(self, 'DataBaseRows'):
            return self.DataBaseRows[item][0]
        else:
            return ""

     
    def OnGetItemImage(self, item):
        if item % 1 == 0:
            return self.idx1
        else:
            return -1

        
    def OnGetItemAttr(self, item):
        if item % 2 == 0:
            return self.attributeColor
        else:
            return None


    def SetMyRows(self, MyRows):
        self.DataBaseRows = MyRows
        self.NumberItems = len(MyRows)
        self.SetItemCount(len(MyRows))

#---------------------------------------------------------------------------
        
class VirtualListPanel1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
                    
        self.list = My_ListCtrlFolders(self)
        sizer.Add(self.list, 1, wx.EXPAND)
        
        self.SetSizer(sizer)
        self.SetAutoLayout(True)
        
#---------------------------------------------------------------------------
        
class My_ListCtrlSupports(wx.ListCtrl,
                          listmix.ListCtrlAutoWidthMixin):     
    def __init__(self, parent, frame):
        wx.ListCtrl.__init__(self, parent, -1,
                             style=wx.LC_REPORT | wx.LC_VIRTUAL |
                             wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
                             wx.LC_VRULES | wx.BORDER_SIMPLE)
        
        listmix.ListCtrlAutoWidthMixin.__init__(self)
        
        #-------------------------------------------------------------------

        fontSize = self.GetFont().GetPointSize()

        # wx.Font(pointSize, family, style, weight, underline, faceName)        
        if wx.Platform == "__WXMAC__":
            self.normalFont = wx.Font(fontSize, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
            self.SetFont(self.normalFont)

        elif wx.Platform == "__WXGTK__":
            self.normalFont = wx.Font(fontSize-1, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
            self.SetFont(self.normalFont)

        else:
            self.normalFont = wx.Font(fontSize-1, wx.DEFAULT, wx.NORMAL, wx.NORMAL)  # fontSize
            self.SetFont(self.normalFont)
            
        #-------------------------------------------------------------------
        
        self.il = wx.ImageList(16, 16)

        bitmapDir = os.path.join(appDir, "bitmaps")
        bmp = wx.Bitmap(os.path.join(bitmapDir, "listCtrl_Cd_Icon.png"))
        # or PyEmbeddedImage
        # bmp = images.ListCtrl_Cd_Icon.GetBitmap()
        self.idx1 = self.il.Add(bmp)
        
        self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
        
        #-------------------------------------------------------------------
        
        self.InsertColumn(0, "Supports", wx.LIST_FORMAT_LEFT, width=199)
        
        #-------------------------------------------------------------------
        
        self.SetItemCount(1000000)
        
        #-------------------------------------------------------------------
        
        self.SetForegroundColour("#6a6a66")
        
        self.attributeColor = wx.ListItemAttr()
        self.attributeColor.SetBackgroundColour(wx.Colour(220, 229, 253))
        
        #-------------------------------------------------------------------
        
        self.SetItemState(0, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
        
        #-------------------------------------------------------------------
        
        # Bind some events to an events handler
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
        self.parent = frame
        self.NumberItems = 0		

    #-----------------------------------------------------------------------

    def SpecialRefresh(self):
        self.Refresh()
        self.parent.text3.SetLabel(_("%d Supports. ") %self.NumberItems)

        
    def OnItemSelected(self, event):
        self.currentItem = event.m_itemIndex
        self.parent.FilterFolders(self.GetItem(self.currentItem, 1).GetText())
        self.parent.SelectedSupport = self.GetItem(self.currentItem, 1).GetText()
        self.parent.SelectedFolder = ""		

        
    def OnItemActivated(self, event):
        self.currentItem = event.m_itemIndex

        
    def getColumnText(self, index, col):
        item = self.GetItem(index, col)
        return item.GetText()
     
    #---------------------------------------------------
    # These methods are callbacks for implementing the
    # "virtualness" of the list...  Normally you would
    # determine the text, attributes and/or image based
    # on values from some external data source, but for
    # this demo we'll just calculate them
    def OnGetItemText(self, item, col):
        if hasattr(self, 'DataBaseRows'):
            return self.DataBaseRows[item][0]
        else:
            return ""

     
    def OnGetItemImage(self, item):
        if item % 1 == 0:
            return self.idx1
        else:
            return -1

         
    def OnGetItemAttr(self, item):
        if item % 2 == 0:
            return self.attributeColor
        else:
            return None


    def SetMyRows(self, MyRows):
        self.DataBaseRows = MyRows
        self.NumberItems = len(MyRows)		
        self.SetItemCount(len(MyRows))
		
#---------------------------------------------------------------------------
     
class VirtualListPanel2(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
            
        self.list = My_ListCtrlSupports(self)
        sizer.Add(self.list, 1, wx.EXPAND)
        
        self.SetSizer(sizer)
        self.SetAutoLayout(True)


